package advanced.sorting;

public class SelectionSort {
	
	public static void selectionSort(int [] intArr)
	{
		int minIndex=0;
		int tempVal=0;
		
		//looping through the array till the second element from the end
		for(int i=0; i<intArr.length-1; i++)
		{
			//looping through the array starting from the next index to find the next minimum value 
			for(int j=i+1; j<intArr.length; j++)
			{
				if(intArr[j]<intArr[minIndex])
				{
					minIndex=j;//save the index of the minimum value
				}
			}
			
			//move the minimum value to the correct place
			tempVal=intArr[i];
			intArr[i]=intArr[minIndex];
			intArr[minIndex]=tempVal;
			System.out.print("During Sorting: ");
			printArr(intArr);
		}
		
		return;
	}
	
	public static void printArr(int[] arr)
	{
		for(int i=0; i<arr.length; i++)
		{
			System.out.print(arr[i]+" ");
		}
		System.out.println();
	}
	
	public static void main(String args[])
	{
		int [] intArr={56,1,64,3,13,22};
		System.out.print("Before: ");
		printArr(intArr);
		SelectionSort.selectionSort(intArr);
		System.out.print("After: ");
		printArr(intArr);
	}

}
